package org.example.javafxdemo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
/**
 * @Author 56465
 * @Create 2024/7/29 16:21
 */
public class WebViewTest extends Application {
    @Override
    public void start(Stage primaryStage) {
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();

        // 加载HTML页面
        webEngine.loadContent("<html><head><p>Hello, World!</p></head><body></body></html>");

        String externalForm = getClass().getResource("styles.css").toExternalForm();
        System.out.println("externalForm = " + externalForm);
        // 动态添加样式表
        webEngine.executeScript("var link = document.createElement('link');"
                + "link.rel = 'stylesheet';"
                + "link.type = 'text/css';"
                + "link.href = '" + externalForm + "';"
                + "document.head.appendChild(link);");

        Scene scene = new Scene(webView, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}